class Record {
  constructor({ id, recordName, recordAge }) {
    this.id = id ? id : 'ID' + getHash(recordName + recordAge);
    this.recordName = recordName;
    this.recordAge = recordAge;
  }

  insertDOM() {
    const recordItems = document.querySelector('#recordItems');
    const recordItem = document.createElement('div');
    recordItem.classList.add('recordItem');
    recordItem.innerHTML = `
        <input class="recordName" type="text">
        <input class="recordAge" type="number" min="0">
        <button class="remove"><i class="far fa-trash-alt"></i></button>
  `;
    recordItem.getElementsByClassName('recordName')[0].value = this.recordName;
    recordItem.getElementsByClassName('recordAge')[0].value = this.recordAge;
    recordItem.id = this.id;
    recordItems.appendChild(recordItem);
  }

  objFilter() {
    return (({ id, recordName, recordAge }) => ({
      id,
      recordName,
      recordAge,
    }))(this);
  }

  insertLocalStorage() {
    let recordInfo = JSON.parse(localStorage.getItem('recordInfo'));
    recordInfo.push(this.objFilter());
    recordInfo = JSON.stringify(recordInfo);
    localStorage.setItem('recordInfo', recordInfo);
  }

  static removeLocalStorage(id) {
    let recordInfo = JSON.parse(localStorage.getItem('recordInfo'));
    const target = recordInfo.findIndex((d) => d.id == id);
    recordInfo.splice(target, 1);
    recordInfo = JSON.stringify(recordInfo);
    localStorage.setItem('recordInfo', recordInfo);
  }

  static updateLocalStorage(newInfo, id) {
    let recordInfo = JSON.parse(localStorage.getItem('recordInfo'));
    let targetIdx = recordInfo.findIndex((d) => {
      return d.id == id;
    });
    recordInfo[targetIdx] = { ...recordInfo[targetIdx], ...newInfo };
    recordInfo = JSON.stringify(recordInfo);
    localStorage.setItem('recordInfo', recordInfo);
  }
}

class RecordsTool {
  constructor(arr) {
    if (arr instanceof Array) this.tks = arr;
    else {
      this.tks = JSON.parse(localStorage.getItem('recordInfo'));
    }
  }

  resetAll() {
    const recordItems = document.getElementById('recordItems');
    while (recordItems.lastChild) {
      recordItems.removeChild(recordItems.lastChild);
    }
  }

  refreshAll(records = this.tks) {
    this.resetAll();
    records.forEach((d) => {
      const t = new Record(d);
      t.insertDOM();
    });
  }
}

getHash = (str) => {
  return str
    .split('')
    .reduce(
      (prevHash, currVal) =>
        ((prevHash << 5) - prevHash + currVal.charCodeAt(0)) | 0,
      0
    );
};

console.log('A')